package com.neutech.maplestory.client;

import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.MapleStoryFrame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
/**
 * ${陈虎}
 * 8:00
 * wp_maplestory_v1.0
 * ${"Do what you say,say what you do"}
 */

public class MapleStoryClient extends MapleStoryFrame{
    public Hero hero = new Hero(this,400,500);

    public List<Arrow> arrows = new CopyOnWriteArrayList<>();

    public List<Mob> mobs= new CopyOnWriteArrayList<>();

    public List<Item> items = new CopyOnWriteArrayList<>();
    public List<Power> powers = new CopyOnWriteArrayList<>();
    public List<Ground> grounds = new CopyOnWriteArrayList<>();
    public List<Flower> flowers = new CopyOnWriteArrayList<>();


    public ItemPackage itemPackage = new ItemPackage(this);
    {
        for (int i = 0; i < 10; i++) {
            Mob mob = new Mob(this,700 + (i * 85),600,5);
            mobs.add(mob);
        }
    }
     {
        for (int i = 0; i < 21; i++) {
            Ground ground = new Ground(this,(i * 90),900,"earth" + i);
            grounds.add(ground);
        }
         for (int i = 0; i < 21; i++) {
             Ground ground = new Ground(this,(i * 90),840,"earth" + i);
             grounds.add(ground);
         }
         for (int i = 0; i < 21; i++) {
             Ground ground = new Ground(this,(i * 90),780,"earth" + i);
             grounds.add(ground);
         }
         for (int i = 0; i < 16; i++) {
             Ground ground = new Ground(this,450+(i * 90),720,"earth" + i);
             grounds.add(ground);
         }
         for (int i = 0; i < 15; i++) {
             Ground ground = new Ground(this,540+(i * 90),660,"earth" + i);
             grounds.add(ground);
         }

         for (int i = 0; i < 3; i++) {
             Ground ground = new Ground(this,200 + (i * 50),540,"flower_2" );
             grounds.add(ground);
         }
         for (int i = 0; i < 2; i++) {
             Ground ground = new Ground(this,205 + (i * 63),575,"flower_3" );
             grounds.add(ground);
         }
         for (int i = 0; i < 3; i++) {
             Ground ground = new Ground(this,450 + (i * 50),390,"flower_2" );
             grounds.add(ground);
         }
         for (int i = 0; i < 2; i++) {
             Ground ground = new Ground(this,455 + (i * 63),425,"flower_3" );
             grounds.add(ground);
         }






         Flower flower = new Flower(this,450,690,"flower" );
         flowers.add(flower);



    }
    {
        for (int i = 0; i < 5; i++) {
            Flower flower = new Flower(this,(i * 90),750,"flower" );
            flowers.add(flower);
        }


        for (int i = 0; i < 16; i++) {
            Flower flower = new Flower(this,540+(i * 90),630,"flower" );
            flowers.add(flower);
        }
        { Flower flower1 = new Flower(this,200,510,"flower_4" );
            flowers.add(flower1);
            Flower flower2 = new Flower(this,300,480,"flower_5" );
            flowers.add(flower2);
            Flower flower3 = new Flower(this,320,500,"flower_6" );
            flowers.add(flower3);}

        {Flower flower1 = new Flower(this,450,360,"flower_4" );
            flowers.add(flower1);
            Flower flower2 = new Flower(this,550,330,"flower_5" );
            flowers.add(flower2);
            Flower flower3 = new Flower(this,570,350,"flower_6" );
            flowers.add(flower3);}

        {Flower flower1 = new Flower(this,540,600,"flower_4" );
            flowers.add(flower1);
            Flower flower2 = new Flower(this,620,570,"flower_5" );
            flowers.add(flower2);
            Flower flower3 = new Flower(this,640,585,"flower_6" );
            flowers.add(flower3);}
        {Flower flower1 = new Flower(this,150,720,"flower_4" );
            flowers.add(flower1);
            Flower flower2 = new Flower(this,250,690,"flower_5" );
            flowers.add(flower2);
            Flower flower3 = new Flower(this,270,710,"flower_6" );
            flowers.add(flower3);}
        {Flower flower1 = new Flower(this,1450,600,"flower_4" );
            flowers.add(flower1);
            Flower flower2 = new Flower(this,1550,570,"flower_5" );
            flowers.add(flower2);
            Flower flower3 = new Flower(this,1570,585,"flower_6" );
            flowers.add(flower3);}
        Flower flower2 = new Flower(this,750,380,"flower_7" );
        flowers.add(flower2);


        Flower flower4 = new Flower(this,480,400,"flower_9" );
        flowers.add(flower4);

    }

    @Override
    public void init() {
        super.init();
        //增加键盘监听器
        this.addKeyListener(new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I){
                    itemPackage.live = !itemPackage.live;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);

            }
        });
    }


    @Override
    public void paint(Graphics g) {



        //迭代器解决线程非安全




        Iterator<Ground> itGrounds = grounds.iterator();
        while (itGrounds.hasNext()){
            Ground ground = itGrounds.next();
            ground.draw(g);

        }
        Iterator<Flower> itFlowers = flowers.iterator();
        while (itFlowers.hasNext()){
            Flower flower = itFlowers.next();
            flower.draw(g);

        }

        for (int i = 0; i < arrows.size(); i++) {
            Arrow arrow =  arrows.get(i);
            arrow.draw(g);
            arrow.hit(mobs);
        }
        Iterator<Mob> itMobs = mobs.iterator();
        while (itMobs.hasNext()){
            Mob mob = itMobs.next();
            mob.draw(g);

        }
        Iterator<Power> itPowers = powers.iterator();
        while (itPowers.hasNext()){
            Power power = itPowers.next();
            power.draw(g);
        }

        Iterator<Item> itItems = items.iterator();
        while (itItems.hasNext()){
            Item item = itItems.next();
            item.draw(g);

        }
        Iterator<Arrow> itArrows = arrows.iterator();
        while (itArrows.hasNext()){
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }
        hero.draw(g);
        itemPackage.draw(g);



        /*for (int i = 0; i < mobs.size(); i++) {
            mobs.get(i).draw(g);
        }*/
//        itemPackage.draw(g);
//
//        Font f = g.getFont();
//        g.setFont(new Font("微软雅黑",Font.BOLD,30));
//        g.drawString("人物的方向为：" + hero.dir,100,100);
//        g.drawString("人物的动作为：" + hero.action,100,150);
//        g.drawString("人物：" + hero.down,100,200);
//        g.drawString("人物的HP：" + hero.HP,100,250);
//        g.drawString("人物的MP：" + hero.MP,100,300);
//        g.setFont(f);
    }


    public static void main(String[] args) {
        new MapleStoryClient().init();
    }
}
